package run.iget.tools.web.response;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import run.iget.tools.web.code.BaseResponseCode;

/**
 * Copyright (C), 2021，行动改变认知[iget.run]
 * 代码千万行，注释第一行
 * ------------ class intro ---------------
 * 对外的api统一返回对象
 *
 * ------------ class intro ---------------
 * @author 大周|450513554@qq.com
 * @since 2020/12/12 21:12:42
 */
@Getter
public class ResponseResult {

    /**
     * 返回码
     */
    private String code;

    /**
     * 返回消息
     */
    private String msg;

    /**
     * 返回实体内容
     */
    private Object data;

    private ResponseResult(){}

    private ResponseResult(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回处理成功后的对象
     * @return this
     */
    public static ResponseResult ok() {
        return ResponseResult.ok(null);
    }

    /**
     * 返回处理成功后的对象
     * @param data -- 响应实际对象
     * @return this
     */
    public static ResponseResult ok(Object data) {
        return ResponseResult.ok(null, data);
    }

    /**
     * 返回处理成功后的对象
     * @param msg -- 响应消息
     * @param data -- 响应实际对象
     * @return this
     */
    public static ResponseResult ok(String msg, Object data) {
        if (StrUtil.isBlank(msg)) {
            msg = BaseResponseCode.SUCCESS.getMsg();
        }
        return new ResponseResult(BaseResponseCode.SUCCESS.getCode(), msg, data);
    }

    /**
     * 返回处理失败的信息
     * @return this
     */
    public static ResponseResult fail(){
        return ResponseResult.fail(BaseResponseCode.FAIL, null);
    }

    /**
     * 返回处理失败的信息
     * @param msg -- 失败消息
     * @return this
     */
    public static ResponseResult fail(String msg){
        return ResponseResult.fail(BaseResponseCode.FAIL, msg);
    }

    /**
     * 返回处理失败的信息
     * @param responseStatus --失败状态吗
     * @return this
     */
    public static ResponseResult fail(BaseResponseCode.ResponseStatus responseStatus){
        return new ResponseResult(responseStatus.getCode(), responseStatus.getMsg(), null);
    }

    /**
     * 返回处理失败的信息
     * @param responseStatus --失败状态吗
     * @param msg --失败消息
     * @return this
     */
    public static ResponseResult fail(BaseResponseCode.ResponseStatus responseStatus, String msg){
        if (StrUtil.isBlank(msg)) {
            msg = responseStatus.getMsg();
        }
        return new ResponseResult(responseStatus.getCode(), msg, null);
    }

}
